﻿using VisionDesigner.HPFeaturePatMatch;
using VM.Common.Helper;

namespace Plugin.Matching
{
    [Serializable]
    public class TemplateParamModel : NotifyPropertyBase
    {
        #region  建立模板参数
        private HPPatCreateScaleMode _scaleMode;
        public HPPatCreateScaleMode ScaleMode
        {
            get { return _scaleMode; }
            set
            {
                Set(ref _scaleMode, value);
                UpdateScaleModeEvent();
            }
        }

        private HPPatCreateThresholdMode _thresholdMode;
        public HPPatCreateThresholdMode ThresholdMode
        {
            get { return _thresholdMode; }
            set
            {
                Set(ref _thresholdMode, value);
                UpdateThresholdModeEvent();
            }
        }

        private Dictionary<string, bool> _paramBool = new Dictionary<string, bool>();
        public Dictionary<string, bool> ParamBool
        {
            get { return _paramBool; }
            set { Set(ref _paramBool, value); }
        }

        private Dictionary<string, int> _paramInt = new Dictionary<string, int>();
        public Dictionary<string, int> ParamInt
        {
            get { return _paramInt; }
            set { Set(ref _paramInt, value); }
        }

        private Dictionary<string, double> _paramDouble = new Dictionary<string, double>();
        public Dictionary<string, double> ParamDouble
        {
            get { return _paramDouble; }
            set { Set(ref _paramDouble, value); }
        }

        private Dictionary<string, Array> _paramArray = new Dictionary<string, Array>();
        public Dictionary<string, Array> ParamArray
        {
            get { return _paramArray; }
            set { Set(ref _paramArray, value); }
        }

        private Dictionary<string, object> _paramEnum = new Dictionary<string, object>();
        public Dictionary<string, object> ParamEnum
        {
            get { return _paramEnum; }
            set
            {
                Set(ref _paramEnum, value);
            }
        }

        #endregion

        public TemplateParamModel()
        {
            // 整型数据
            ParamInt.Add("特征尺度", 3);
            ParamInt.Add("对比度阈值", 20);
            // 浮点型数据
            ParamDouble.Add("速度尺度", 3.0);
            //  Enum数据
            ParamArray.Add("尺度模式", Enum.GetValues(typeof(HPPatCreateScaleMode)));
            ScaleMode = HPPatCreateScaleMode.自动;
            ParamArray.Add("阈值模式", Enum.GetValues(typeof(HPPatCreateThresholdMode)));
            ThresholdMode = HPPatCreateThresholdMode.自动;

        }

        #region 方法

        /// <summary>
        /// 从界面配置更新至Para对象
        /// </summary>
        /// <param name="patternCreateParas"></param>
        public void UpdatePatCreateParas(CHPFeaturePattern cHPFeaturePattern)
        {
            cHPFeaturePattern.SetRunParam("PyramidScaleFlag", Convert.ToInt32(ScaleMode).ToString());
            if (HPPatCreateScaleMode.手动 == ScaleMode)
            {
                cHPFeaturePattern.SetRunParam("PyramidScaleRough", ParamDouble["速度尺度"].ToString("#0.0"));
                cHPFeaturePattern.SetRunParam("PyramidScaleFine", ParamInt["特征尺度"].ToString());
            }
            cHPFeaturePattern.SetRunParam("EdgeThresholdFlag", Convert.ToInt32(ThresholdMode).ToString());
            if (HPPatCreateThresholdMode.手动 == ThresholdMode)
            {
                cHPFeaturePattern.SetRunParam("EdgeThreshold", ParamInt["对比度阈值"].ToString());
            }
        }

        #endregion

        #region

        // 检查 "阈值模式" 是否为自动，更新 IsThresEnabled 的值
        private void UpdateScaleModeEvent()
        {
            ParamBool["尺度模式"] = ScaleMode == HPPatCreateScaleMode.手动;
            // 触发通知以刷新UI
            RaisePropertyChanged(nameof(ParamBool));
        }

        private void UpdateThresholdModeEvent()
        {
            ParamBool["阈值模式"] = ThresholdMode == HPPatCreateThresholdMode.手动;
            // 触发通知以刷新UI
            RaisePropertyChanged(nameof(ParamBool));
        }

        #endregion
    }

    [Serializable]
    public enum HPPatCreateScaleMode
    {
        自动 = 1,
        手动 = 0
    }

    [Serializable]
    public enum HPPatCreateThresholdMode
    {
        自动 = 1,
        手动 = 0
    }
}
